﻿#define _CRT_SECURE_NO_WARNINGS 1

//动态规划第六题
//63. 不同路径 II
//https://leetcode.cn/problems/unique-paths-ii/description/

class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
        int m = obstacleGrid.size();
        int n = obstacleGrid[0].size();
        // 状态表示
        // dp[i][j]结尾仍然表示到该位置的几种方式
        vector<vector<int>> dp(m + 1, vector<int>(n + 1));

        // 初始化
        dp[1][0] = 1;

        // 填表
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                // 下标映射
                // dp表多一行，grid下标要减去1
                if (obstacleGrid[i - 1][j - 1] == 0)
                    dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            }
        }

        return dp[m][n];
    }
};